package com.onlinepay.service.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.onlinepay.entity.QueryListParams;
import com.onlinepay.entity.QueryParams;
import com.onlinepay.exception.EncryptException;
import com.onlinepay.service.PaymentService;
import com.onlinepay.util.MD5;
import com.onlinepay.util.RSAUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @Author: linjl
 * @Date: 2025/3/12 14:54
 * @Description:
 */

@Slf4j
@Service
public class PaymentServiceImpl implements PaymentService {


    public String payment(JSONObject map,String paymentUrl,String refererUrl,String paymentType) throws EncryptException,Exception {

        getSign(map,map.getString("sign"));
        long start = System.currentTimeMillis();
        HttpResponse execute = HttpRequest.post(paymentUrl).header("paymenttype", paymentType).header("referer", refererUrl).body(JSONObject.toJSONString(map)).execute();
        long end = System.currentTimeMillis();
        System.out.println("---毫秒值" + (start - end));
        return execute.body();
    }

    @Override
    public String query(QueryParams cryptoQueryParams, String queryUrl, String refererUrl) throws EncryptException,Exception {

        JSONObject map = JSON.parseObject(JSON.toJSONString(cryptoQueryParams));

        //生成签名
        String checkMd5Src = String.valueOf(map.get("merNo")) +
                map.get("merOrderNo") +
                map.get("tradeNo") +
                map.get("queryType") +
                map.get("privateKey");
        String checkMd5 = new MD5().getMD5ofStr(checkMd5Src);
        map.put("sign", checkMd5);

        long start = System.currentTimeMillis();
        HttpResponse execute = HttpRequest.post(queryUrl).header("referer", refererUrl).body(JSONObject.toJSONString(map)).execute();
        long end = System.currentTimeMillis();
        System.out.println("---毫秒值" + (start - end));
        return execute.body();
    }

    @Override
    public String queryList(QueryListParams cryptoQueryListParams, String queryUrl, String refererUrl) throws EncryptException, Exception {

        JSONObject map = JSON.parseObject(JSON.toJSONString(cryptoQueryListParams));

        String checkMd5Src = String.valueOf(map.get("merNo")) +
                map.get("privateKey");

        String checkMd5 = new MD5().getMD5ofStr(checkMd5Src);
        map.put("sign", checkMd5);
        long start = System.currentTimeMillis();
        HttpResponse execute = HttpRequest.post(queryUrl).header("referer", refererUrl).body(JSONObject.toJSONString(map)).execute();
        long end = System.currentTimeMillis();
        System.out.println("---毫秒值" + (start - end));
        return execute.body();
    }

    @Override
    public String refund(JSONObject map, String refundUrl, String refererUrl) throws EncryptException, Exception {

        getSign(map,map.getString("sign"));
        long start = System.currentTimeMillis();
        HttpResponse execute = HttpRequest.post(refundUrl).header("referer", refererUrl).body(JSONObject.toJSONString(map)).execute();
        long end = System.currentTimeMillis();
        System.out.println("---毫秒值" + (start - end));
        return execute.body();
    }

    private void getSign(JSONObject map,String privateKey) throws Exception {
        map.remove("sign");
        String treeValue = RSAUtil.getTreeValue(map, false);
        System.out.println(treeValue);
        String sign = RSAUtil.signSHA256RSA(treeValue, privateKey, "UTF-8");
        map.put("sign", sign);
    }
}
